package com.ruoyi.web.controller.spoken;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.SpokenUser;
import com.ruoyi.system.domain.model.MiniRegisterBody;
import com.ruoyi.system.service.ISpokenUserCourseTimeService;
import com.ruoyi.system.service.ISpokenUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@Api(description = "用户管理")
@RestController
@RequestMapping("/spoken/user")
public class UserController extends BaseController {

    @Autowired
    ISpokenUserService spokenUserService;

    @Autowired
    TokenService tokenService;

    @Autowired
    ISpokenUserCourseTimeService userCourseTimeService;

    /**
     * 用户微信授权注册
     * @param miniRegisterBody
     * @return
     */
    @ApiOperation("小程序授权注册")
    @PostMapping("/register")
    public AjaxResult register(@RequestBody MiniRegisterBody miniRegisterBody){
        // 小程序授权注册
        SpokenUser spokenUser = spokenUserService.wechatRegister(miniRegisterBody);
        LoginUser loginUser = new LoginUser();
        loginUser.setUserId(spokenUser.getUserId());
        loginUser.setUser(spokenUser.getSysUser());

        // 获取token
        String token = tokenService.createToken(loginUser);
        Map result = new HashMap();
        result.put("token", token);
        result.put("userId", spokenUser.getUserId());

        return success(result);
    }

    @ApiOperation("小程序登录")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody Map param){
        String code = (String)param.get("code");
        SpokenUser spokenUser = spokenUserService.wechatLogin(code);
        // 用户被停用
        if(spokenUser != null && spokenUser.getSysUser().getStatus().equals("1")){
            return AjaxResult.error(HttpStatus.IS_BAN, "当前用户已被停用，请联系管理员");
        }
        Map result = new HashMap();
        // 注册过，直接登录
        if(spokenUser != null){
            LoginUser loginUser = new LoginUser();
            loginUser.setUserId(spokenUser.getUserId());
            loginUser.setUser(spokenUser.getSysUser());
            String token = tokenService.createToken(loginUser);
            result.put("token", token);
            result.put("userId", spokenUser.getUserId());
        }
        return success(result);
    }

    @GetMapping
    @ApiOperation("查询用户信息")
    public AjaxResult getUserInfo(){
        Long userId = SecurityUtils.getUserId();
        return success(spokenUserService.selectById(userId));
    }

    @GetMapping("/getPhoneNumber")
    @ApiOperation("获取手机号")
    public AjaxResult getPhoneNumber(String code){
        String wechatPhoneNumber = spokenUserService.getWechatPhoneNumber(code);
        Map phone = new HashMap();
        phone.put("phoneNumber", wechatPhoneNumber);
        return success(phone);
    }

    @PutMapping
    @ApiOperation("修改用户信息")
    public AjaxResult edit(@RequestBody SpokenUser user){
        return success(spokenUserService.update(user));
    }

    @GetMapping("/list")
    @ApiOperation("用户列表")
    public TableDataInfo list(SpokenUser user){
        startPage();
        return getDataTable(spokenUserService.selectList(user));
    }

    @GetMapping("/courseTimeList")
    @ApiOperation("查询用户选择的课程表日期")
    public AjaxResult courseTimeList(){
        return success(spokenUserService.selectUserCourseTimeDate());
    }

    @GetMapping("/courseTimeInfo/{chooseDate}")
    @ApiOperation("对应日期的课时详情")
    public AjaxResult courseTimeInfo(@PathVariable String chooseDate){
        return success(spokenUserService.selectCourseTimeInfoByDate(chooseDate));
    }

    @GetMapping("/cancelLesson/{uctId}")
    @ApiOperation("消课")
    public AjaxResult cancelLesson(@PathVariable Long uctId){
        return success(userCourseTimeService.cancelLessonNum(uctId));
    }

    @GetMapping("/leaveLesson/{uctId}")
    @ApiOperation("请假")
    public AjaxResult leaveLesson(@PathVariable Long uctId){
        return success(userCourseTimeService.leaveLesson(uctId));
    }
}
